package gq.example.mapper;

import com.github.pagehelper.Page;
import gq.example.annotation.AutoFill;
import gq.example.dto.FeedbackPageQueryDto;
import gq.example.entity.Feedback;
import gq.example.entity.QuestionAndFeedback;
import gq.example.enumeration.OperationType;
import gq.example.vo.CommunicationVo;
import gq.example.vo.FeedbackVo;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FeedbackMapper {

    /**
     * 分页查询
     * @param feedbackPageQueryDto
     * @return
     */
    Page<FeedbackVo> pageQuery(FeedbackPageQueryDto feedbackPageQueryDto);

    /**
     * 回复意见
     * @param feedback
     */
    @Options(useGeneratedKeys = true, keyProperty = "id",  keyColumn = "id") //回填主键id
    @AutoFill(value = OperationType.INSERT)
    @Insert("insert into feedback (description,create_user,create_time) values (#{description},#{createUser},#{createTime})")
    void addFeedback(Feedback feedback);


    /**
     * 建立意见与回复的关系
     * @param questionAndFeedback
     */
    @Insert("insert into question_feedback(is_read, question_id, feedback_id, proposer_id,replier_id,proposer_name, replier_name,create_time) " +
            "VALUES (#{isRead},#{questionId},#{feedbackId},#{proposerId},#{replierId},#{proposerName},#{replierName},#{createTime})")
    void buildRelation(QuestionAndFeedback questionAndFeedback);

    /**
     * 未读回复
     * @param createUser // 提出者id
     * @return
     */
    @Select("select count(*) from question_feedback where proposer_id = #{createUser} and proposer_id <> question_feedback.replier_id and is_read = 0")
    Integer countReplies(Long createUser);

    /**
     * 未读追问
     * @param createUser // 回复者id
     * @return
     */
    @Select("select count(*) from question_feedback qf where qf.is_read = 0 and qf.proposer_id = qf.replier_id and not (#{createUser} = qf.proposer_id)  and exists " +
            "(select 1 from question_feedback qf0 inner join question q on qf0.question_id = q.id " +
            "WHERE q.sector_id = #{sectorId} " +
            "and qf0.replier_id = #{createUser} " +
            "and qf0.question_id = qf.question_id " +
            "and qf0.proposer_id = qf.proposer_id) " +
            "and qf.create_time > (select MAX(create_time) " +
            "from question_feedback where question_id = qf.question_id and replier_id = #{createUser})")
    Integer countProbe(Long createUser, Integer sectorId);

    /**
     * 回复查看(详细)
     * @param id // 问题id
     * @param
     * @return
     */
    @Select("select f.description, f.create_time as createTime, qf.replier_id as createUser " +
            "from feedback as f inner join question_feedback as qf on f.id = qf.feedback_id " +
            "where qf.question_id = #{id} order by qf.create_time asc")
    List<CommunicationVo> viewReply(@Param("id") Integer id);

    /**
     * 更新意见与回复的关系（回复者点击，提出者追问全部已读）#意见列表、回复查看中进入对话都适用
     * @param id // 问题id
     */
    @Update("update question_feedback set is_read = 1 where question_id = #{id}  and proposer_id = replier_id")
    void updateRelationReplier(Integer id);

    /**
     * 更新意见与回复的关系（提出者点击，回复全部已读）
     * @param id // 问题id
     */
    @Update("update question_feedback set is_read = 1 where question_id = #{id}  and proposer_id <> replier_id")
    void updateRelationProposer(Integer id);

    /**
     * 获取问题
     * @param createUser //当前用户 #true即为提问者
     * @param id //问题id
     * @return
     */
    @Select("select exists(select 1 from question_feedback where question_id = #{id} and proposer_id = #{createUser})")
    Boolean existsProposerIdMatch(@Param("id") Integer id, @Param("createUser") Long createUser);

    /**
     * 判断是否存在未读
     * @param id
     * @return
     */
    @Select("select exists(select 1 from question_feedback where question_id = #{id} and  is_read = 0) ")
    Boolean existsFeedbackMatch(Integer id);

    /**
     * 判断是否归档
     * @param questionId
     * @return
     */
    @Select("select exists(select  1 from question where id = #{questionId} and status = 3)")
    boolean isArchived(Integer questionId);

}
